﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ArgusLib.Controls;
using ArgusLib.Drawing;

namespace Fraktalysator.Forms
{
	partial class ImageForm : ImageDisplayForm
	{
		#region Fields
		RectangleSelectionLayer selectionLayer;
		Size realImageSize;
		#endregion

		#region Properties
		public float RectangleRatio { get { return this.selectionLayer.RectangleRatio; } set { this.selectionLayer.RectangleRatio = value; } }
		public ZoomDirection ZoomDirection { get; private set; }

		public RectangleA Selection
		{
			get
			{
				RectangleA sel = this.selectionLayer.Selection;
				sel.Width /= this.Magnification;
				sel.Height /= this.Magnification;
				sel.X /= this.Magnification;
				sel.Y /= this.Magnification;
				return sel;
			}
		}

		public float Magnification
		{
			get
			{
				return this.cbMagnification.Value;
			}
			set
			{
				if (value > 0 && value <= 1)
					this.cbMagnification.Value = value;
			}
		}

		public override Size ImageSize
		{
			get
			{
				return this.realImageSize;
			}
			set
			{
				this.realImageSize = value;
				Size size = new Size(
					(int)(value.Width * this.Magnification),
					(int)(value.Height * this.Magnification));
				float mag = this.Magnification;

				if (size.Width < this.MinimumClientSize.Width)
					mag = this.MinimumClientSize.Width / (float)value.Width;
				if (size.Height < this.MinimumClientSize.Height)
					mag = Math.Max(mag, this.MinimumClientSize.Height / value.Height);

				if (mag == this.Magnification)
					base.ImageSize = size;
				else
					this.cbMagnification.Text = mag.ToString("p0");
			}
		}
		#endregion

		#region Constructors
		public ImageForm()
			:base()
		{
			InitializeComponent();
			this.ImageContainer.Margin = new Padding(0, this.stackPanel.Bottom, 0, 0);
			this.ImageContainer.Location = new Point(0, this.stackPanel.Bottom);
			this.ImageContainer.BorderStyle = BorderStyle.Fixed3D;
			this.selectionLayer = new RectangleSelectionLayer();
			this.selectionLayer.Dock = DockStyle.Fill;
			this.selectionLayer.SelectionMode = ArgusLib.Controls.SelectionMode.FullControl;
			this.selectionLayer.Cursor = Cursors.Cross;
			this.ImageContainer.Controls.Add(this.selectionLayer);
			this.ImageLayout = ImageLayout.Zoom;
			this.cbZoomIn.Checked = true;
			this.MinimumClientSize = new Size(
				this.cbMagnification.Right + this.cbMagnification.Margin.Right,
				this.stackPanel.Bottom);
			this.cbMagnification.ValueChanged += cbMagnification_ValueChanged;
		}
		#endregion

		#region Non-Public Methods
		protected override void OnLoad(EventArgs e)
		{
			this.cbZoomIn.Image = Resources.Icons16.MagnifierZoomIn;
			this.cbZoomOut.Image = Resources.Icons16.MagnifierZoomOut;
			base.OnLoad(e);
		}

		protected override void OnResizeEnd(EventArgs e)
		{
			this.realImageSize.Width = (int)(base.ImageSize.Width / this.Magnification);
			this.realImageSize.Height = (int)(base.ImageSize.Height / this.Magnification);
			base.OnResizeEnd(e);
		}

		protected override void OnSizeChanged(EventArgs e)
		{
			if (ArgusLib.Environment.Runtime == ArgusLib.Runtime.Mono)
			{
				this.realImageSize.Width = (int)(base.ImageSize.Width / this.Magnification);
				this.realImageSize.Height = (int)(base.ImageSize.Height / this.Magnification);
			}
			base.OnSizeChanged(e);
		}

		private void cbZoomIn_CheckedChanged(object sender, EventArgs e)
		{
			this.cbZoomOut.Checked = !this.cbZoomIn.Checked;
			if (this.cbZoomIn.Checked == true)
				this.ZoomDirection = ZoomDirection.ZoomIn;
			else
				this.ZoomDirection = ZoomDirection.ZoomOut;
		}

		private void tsZoomOut_CheckedChanged(object sender, EventArgs e)
		{
			this.cbZoomIn.Checked = !this.cbZoomOut.Checked;
			if (this.cbZoomIn.Checked == true)
				this.ZoomDirection = ZoomDirection.ZoomIn;
			else
				this.ZoomDirection = ZoomDirection.ZoomOut;
		}

		void cbMagnification_ValueChanged(object sender, EventArgs e)
		{
			if (this.Magnification > 0 && this.Magnification <= 1)
				this.ImageSize = this.ImageSize;
		}
		#endregion

		#region Events
		public event EventHandler RectangleSelected
		{
			add { this.selectionLayer.RectangleSelected += value; }
			remove { this.selectionLayer.RectangleSelected -= value; }
		}

		public event EventHandler SelectionChanged
		{
			add { this.selectionLayer.SelectionChanged += value; }
			remove { this.selectionLayer.SelectionChanged -= value; }
		}

		public event EventHandler SelectionChangingBegin
		{
			add { this.selectionLayer.SelectionChangingBegin += value; }
			remove { this.selectionLayer.SelectionChangingBegin -= value; }
		}

		public event EventHandler ImageMouseEnter
		{
			add { this.selectionLayer.MouseEnter += value; }
			remove { this.selectionLayer.MouseEnter -= value; }
		}

		public event EventHandler ImageMouseLeave
		{
			add { this.selectionLayer.MouseLeave += value; }
			remove { this.selectionLayer.MouseLeave -= value; }
		}

		public event MouseEventHandler ImageMouseMove
		{
			add { this.selectionLayer.MouseMove += value; }
			remove { this.selectionLayer.MouseMove -= value; }
		}

		//public event EventHandler MagnificationChanged
		//{
		//	add { this.cbMagnification.ValueChanged += value; }
		//	remove { this.cbMagnification.ValueChanged -= value; }
		//}
		#endregion
	}
}
